package com.xinghou.platform.service.main.impl;

import com.xinghou.platform.dao.main.MeijiDAO;
import com.xinghou.platform.entity.req.ListReq;
import com.xinghou.platform.model.main.Meiji;
import com.xinghou.platform.model.main.MeijiWaterfall;
import com.xinghou.platform.server.api.common.ImageComb;
import com.xinghou.platform.service.main.MeijiService;
import com.xinghou.platform.util.PhotoPathUtil;
import com.xinghou.platform.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by liufangyu on 2016/8/30.
 */
@Service("MeijiService")
public class MeijiServiceImpl implements MeijiService {
    @Resource
    private MeijiDAO meijiDAO;

    @Override
    public List<Meiji> meijiList(ListReq listReq){
       List<Meiji>  list =meijiDAO.meijiList(listReq);
        for(Meiji meiji :list) {
            meiji.setPhoto(combPrefix(meiji.getPhoto()));
            meiji.setSmallphoto(smallCombPrefix(meiji.getPhoto()));

        }
        return list;
    }

    @Override
    public List<MeijiWaterfall> waterfallList(ListReq listReq){
        List<MeijiWaterfall>  list =meijiDAO.waterfallList(listReq);
        for(MeijiWaterfall meijiWaterfall :list) {
            meijiWaterfall.setPhoto(combPrefix(meijiWaterfall.getPhoto()));
            meijiWaterfall.setSmallphoto(smallCombPrefix(meijiWaterfall.getPhoto()));
        }
        return list;
    }

    @Override
    public Meiji meijiDetail(Integer id){
       Meiji meiji =meijiDAO.getMeijiById(id);
       String photo =meiji.getPhoto();
        meiji.setPhoto(combPrefix(photo));
        meiji.setSmallphoto(smallCombPrefix(photo));
        return meiji;
    }


    public String combPrefix( String url) {
        String result = "";
        if (StringUtils.isEmpty(url)) return "";
        String[] urls = url.split(",");
        for (int i = 0; i < urls.length; i++) {
                result = result + (ImageComb.comPrefixUrl(urls[i]));
            if (i != urls.length - 1)
                result = result + ",";
        }
        return result;
    }

    public String smallCombPrefix( String url) {
        String result = "";
        if (StringUtils.isEmpty(url)) return "";
        String[] urls = url.split(",");
        for (int i = 0; i < urls.length; i++) {
            result = result + (PhotoPathUtil.getMeiji150w150h(ImageComb.comPrefixUrl(urls[i])));
            if (i != urls.length - 1)
                result = result + ",";
        }
        return result;
    }
}
